Ext.define("App.report.iteration.view.IterationReportEditor", {
    extend: 'Extend.window.FormWindow',
    alias: ['widget.IterationReportEditor'],
    requires: ['App.report.model.KpiModel',
        'App.iteration.field.IterationComboBox',
        'App.reportconfiguration.field.ReportConfigurationComboBox',
        'App.report.view.KpisGridField'],
    config: {
        unmaskable: true,
        entity: 'entity',
        defaults: {
            columnWidth: 1
        },
        window: {
            title: '报表填写',
            width: 800,
            height: 500,
            resizable: false
        }
    },
    items: [{
        xtype: 'hidden',
        name: 'id'
    }, {
        xtype: 'hidden',
        name: 'parentType'
    }, {
        xtype: 'hidden',
        name: 'reportConfigurationId'
    }, {
        xtype: 'IterationComboBox',
        name: 'parentId',
        fieldLabel: '迭代',
        allowBlank: false,
        blankText: '迭代为必填字段，不允许为空！',
        readOnly: true,
        // listeners: {
        //     change: function ($this, newValue, oldValue, eOpts) {
        //         var store = this.up().down('gridfield[name="kpis"]').getStore();
        //         var line = store.findRecord('field', 'iteration');
        //         if (!Ext.isEmpty(line)) {
        //             line.set('value', this.getSelection().get('number'));
        //         }
        //     }
        // }
    }, {
        name: 'kpis',
        xtype: 'KpiGridField',
        fieldLabel: '指标录入',
        columnWidth: 1,
        roweditable: true,
        height: 330
    }],
    loadRecord: function (record) {
        var me = this;
        if (Ext.isEmpty(record.get('id'))) {
            me.down('IterationComboBox').setReadOnly(false);
            var lines = [];
            var rckpi = me.reportConfiguration.kpis;
            for (var i = 0; i < rckpi.length; i++) {
                var rckpi = rckpis[i];
                var kc = rckpi[i].kpiConfiguration;
                lines.push({
                    field: kc.field,
                    value: null,
                    dataType: kc.dataType,
                    automaticCalculation: kc.automaticCalculation
                });
            }
            record.set('kpis', lines)
        }
        me.callParent(arguments);
    },
    listeners: {
        load: function ($this, action) {
            var me = this;
            var rckpis = me.reportConfiguration.kpis;
            var kpis = action.result.data.kpis;
            var sortKpis = [];
            for (var i = 0; i < rckpis.length; i++) {
                var kc = rckpis[i].kpiConfiguration;
                var rckpi = rckpis[i];
                var kpi = Ext.Array.findBy(kpis, function (item) {
                    if (item.field == kc.field) {
                        return item;
                    }
                });
                if (!Ext.isEmpty(kpi)) {
                    kpi.field = kc.field;
                    kpi.dataType = kc.dataType;
                    kpi.automaticCalculation = rckpi.automaticCalculation;
                    sortKpis.push(kpi);
                } else {
                    var newKpi = {
                        field: kc.field,
                        dataType: kc.dataType,
                        value: null,
                        automaticCalculation: rckpi.automaticCalculation
                    };
                    sortKpis.push(newKpi);
                }
            }
            action.result.data.kpis = sortKpis;
        }
    }
});